package com.seaside.park.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seaside.park.component.ReadYZNo;
import com.seaside.parkCommon.bean.ParkZone;
import com.seaside.parkCommon.mapper.ParkZoneMapper;
import com.seaside.parkCommon.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class RemoteService {
    @Autowired
    private ParkZoneMapper parkZoneMapper;

    public void changeLampStatus(String zoneId, String parkNo, String status) {
        //编号转化成序号，和云智统一
        ParkZone parkZone = ReadYZNo.yzcodeProfile.get(zoneId);
        try {
            if (parkZone.getZonePort() != 0) {
                Map<String, String> paramMap = new HashMap();
                paramMap.put("parkZoneNo", parkZone.getZoneNo());
                paramMap.put("parkNo", parkNo);
                paramMap.put("switchStatus", status);
                JSONObject retTemp = JSON.parseObject(HttpUtil.doPostForm("http://175.178.225.26:" + parkZone.getZonePort() + "/changeLampSwitchRemote", paramMap));
                if (retTemp.getBoolean("sucess")) {
                    log.info(parkZone.getZoneName() + "远程打开预约灯光成功!");
                } else {
                    log.error(parkZone.getZoneName() +"远程打开预约灯光失败!");
                }
            }
        } catch (Exception e) {
            log.error(parkZone.getZoneName() +"远程打开预约灯光出现错误!" + e.getMessage());
        }
    }

    /**
     * 车辆离位清空预约清单
     * @param zoneId
     * @param parkNo
     * @param carPlate
     */

    public void leavePark(String zoneId, String parkNo, String carPlate) {
        //编号转化成序号，和云智统一
        ParkZone parkZone = ReadYZNo.yzcodeProfile.get(zoneId);
        try {
            if (parkZone.getZonePort() != 0) {
                Map<String, String> paramMap = new HashMap();
                paramMap.put("parkNo", parkNo);
                paramMap.put("carPlate", carPlate);
                JSONObject retTemp = JSON.parseObject(HttpUtil.doPostForm("http://175.178.225.26:" + parkZone.getZonePort() + "/leavePark", paramMap));
                log.info("通知离位的通知地址返回:" + retTemp);
                if (retTemp.getBoolean("sucess")) {
                    log.info(parkZone.getZoneName() + "离位远程通知成功!");
                } else {
                    log.error(parkZone.getZoneName() + "离位远程通知失败!");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(parkZone.getZoneName() + "离位远程通知出现错误!" + e.getMessage());
        }
    }
}
